﻿<?php 

	
	$gdt=new gdt();

	$gdt->gd();

	class gdt{

		public function gd(){


			$token='171887411595049713';   //广告主线索平台token
			$secret='ceabc0d2cc630c37be55c6cb23b4161b';    //广告主线索平台生成的接口密钥
			$timestamp=time();   //即当前的秒级时间戳
			$signature=base64_encode($token . "," . $timestamp . "," .sha1($token.".".$timestamp.".".$secret));			
			$url='https://leads.qq.com/api/mv1/leads/list';//http://leads.qq.com/api/mv1/leads/list
			$params=['start_time'=>'2020-07-16','end_time'=>'2020-07-18','page'=>1,'page_size'=>10];
			$header=["X-Signature:{$signature}",'Accept:application/json'];
			//header("X-Signature:{$signature}");
			//var_dump(self::php_do_Url_GET($url,$params,$header));

			$auth='Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJjb21wYW55X2lkIjoiYzNhNTBhamxrNzVyZSIsIm5hbWUiOiIxMDA1IiwiaXNfYWRtaW4iOjEsImV4cCI6MTU5NTI3MDkxMn0.ylqs9OhZ8pMHkx4npFn3K8JIAxMr063OCWV9Y0j706o';

			$result=self::php_do_Url_POST(
				 'https://api.yunque360.com/v1/worker/wxuser/index'
				,['page'=>1,'order_by'=>'last_event_time','pagesize'=>1000]
				,['Authorization: '.$auth]);

			

			$result1=self::php_do_Url_POST(
				 'https://api.yunque360.com/v1/company/conversation/wechat-history'
				,['open_id'=>'oW2Wk1UwmegpOIeCKBanOEvWj8Ys']
				,['Authorization: '.$auth]);


			$result2=self::php_do_Url_GET(
				 'https://api.yunque360.com/v1/worker/user/info'
				,['uid'=>'ohA_w09Nhq_IMhbFYCRzihZh7Ka4']
				,['Authorization: '.$auth]);
			
			echo '<pre>';

				//var_dump($result2['result']['name']);

				//var_dump($result2['result']['mobile']);

				//var_dump($result2['result']['showname']);

				//var_dump($result2['result']['remark']);

				var_dump($result['result']);

				//var_dump($result1);


			echo '</pre>';

			// $date=json_decode($input,true);

			// $key=array_keys($date);

			// $value=array_values($date);

			// $keystr="(".implode($key, ",").")";

			// $valuestr="('".implode($value, "','")."')";

			// $query="insert into gdtphone ".$keystr." value ".$valuestr;

			// $result=self::insertDb($query);

			// if($result['result']){

			// 	$CusName=$date['leads_name'];
			// 	$CusPhone=$date['leads_tel'];
			// 	$ChannelID='1284016882717954048';
			// 	$CusAddress=$date['address'];
			// 	$Remark='';
			// 	self::api($CusName,$CusPhone,$ChannelID,$CusAddress,$Remark);

			// }else{

			// 	file_put_contents('phone.json',"电话：".$date['leads_tel']."ID：".$date['leads_id']."erro:".$result['erro']."时间：".date("Y-m-d H:i:s").PHP_EOL,FILE_APPEND);
			// }

			// echo 'hello qm';


		}

		public function insertDb($query){


	        $host='localhost';
	        $user='root';
	        $password='root';
	        $database='weizandata';
	        $link=mysqli_connect($host, $user, $password, $database);
	        $link->query("set names utf8");
	        $result=mysqli_query($link, $query);
	        //@$arr=mysqli_fetch_all($result, 1);

	        //echo mysqli_error($link);

	        return ["result"=>$result,"row"=>mysqli_affected_rows($link),'erro'=>mysqli_error($link)];



		}

	    public function api($CusName,$CusPhone,$ChannelID,$CusAddress,$Remark)
   		{


	        $data["CusName"]= $CusName;
	        $data["CusPhone"]=$CusPhone;
	        $data["ChannelID"]= $ChannelID;
	        $data["CusAddress"]= $CusAddress;
	        $data["Remark"]=$Remark;
	        $data = json_encode($data);
	        $pp["Service"] = "com.QM.CallCenter.CommonChannelAPI";
	        $pp["Param"] = addslashes($data);

	        $pp = json_encode($pp);
	        $pp = str_replace('\\\\\\',"\\",$pp);

	        $out = self::do_curl("https://capi.qumei.com/api/home/call",$pp,false,"POST");
	        $out = json_decode($out,true);

	        if ($out['statusCode']==200){

	            return 1;

	        }else{

	            return 2;

	        }


   		}


	    public function do_curl($url, $params = array(), $upload = false, $type = 'POST')
	    {

	        $method = strtoupper($type);
	        if ($method == 'GET') {
	            $url = "{$url}?" . http_build_query($params);
	        }
	        $ch = curl_init();
	        //curl_setopt($ch, CURLINFO_HEADER_OUT, true);
	        //输出主机头
	        if ($url=="https://capi.qumei.com/api/home/call"){
	            //定义请求类型
	            $username="webuser";
	            $password="Qumei1993";
	            $timestamp=time();

	            $sign = md5($username.",".$password.",".$timestamp);

	            $token = file_get_contents("token.txt");

	            $headers = array(
	                "Content-Type: application/json",
	                "Authorization: Bearer $token",
	                "username: $username",
	                "password: $password",
	                "timestamp: $timestamp",
	                "sign: $sign"
	            );

	            curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
	        }

	        curl_setopt($ch, CURLOPT_URL, $url);
	        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
	        //curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
	        // 是否检测服务器的证书是否由正规浏览器认证过的授权CA颁发的
	        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
	        // 是否检测服务器的域名与证书上的是否一致
	        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);

	        if ($method == 'POST') {
	            if ($upload) {        //设置上传文件
	                $file = new \CURLFile($upload['tmp_name'], $upload['type'], $upload['name']);
	                $params[$upload['get_name']] = $file;
	            }
	            //$params = '{"Service":"com.QM.CallCenter.CommonChannelAPI","Param":{\"CusName\":\"test591\u5ba2\u8d44\uff08\u8bf7\u5ffd\u7565\uff09\",\"CusPhone\":\"15107984564\",\"ChannelID\":\"1262592253055078400\",\"CusAddress\":\"\u5317\u4eac\",\"Remark\":\"\"}}';
	            @curl_setopt($ch, CURLOPT_POST, 1);
	            @curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
	        }

	        $result = curl_exec($ch);
	        //$request_header = curl_getinfo( $ch, CURLINFO_HEADER_OUT);
	        //print_r($request_header);
	        //打印发送头信息

	        curl_close($ch);
	        if ($result === null || $result === false) {

	            $this->error('操作出错！');

	        } else {

	            return $result;

	        }

	    }

	    public function  php_do_url_POST($url,$params,$headers){ //POST get

			$ch = curl_init ();

			curl_setopt ( $ch, CURLOPT_URL, $url );
			curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );
			curl_setopt ( $ch, CURLOPT_CUSTOMREQUEST, 'POST' );
			curl_setopt ( $ch, CURLOPT_POSTFIELDS, $params );
			curl_setopt ( $ch, CURLOPT_HTTPHEADER, $headers );
			curl_setopt ( $ch, CURLOPT_TIMEOUT, 60 );
			curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
			curl_setopt($ch, CURLOPT_SSLVERSION, false);
			curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

			$json_string = curl_exec ( $ch );
			$data = json_decode($json_string, true);
			curl_close ( $ch );
			return $data;
		}

		public function php_do_Url_GET($url,$params=[],$headers=[]){ //Get get

			$url = "{$url}?" . http_build_query ($params);
			//echo $url;
			$ch = curl_init ();
			curl_setopt ( $ch, CURLOPT_URL, $url );
			curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );
			curl_setopt ( $ch, CURLOPT_CUSTOMREQUEST, 'GET' );
			curl_setopt ( $ch, CURLOPT_TIMEOUT, 60 );
			//curl_setopt ( $ch, CURLOPT_POSTFIELDS, $params );
			curl_setopt ( $ch, CURLOPT_HTTPHEADER, $headers );
			curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
			curl_setopt($ch, CURLOPT_SSLVERSION, false);
			curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);


			$json_string = curl_exec ( $ch );

			$data = json_decode($json_string, true);

			curl_close ( $ch );

			return $data;
		}

		public function curl_get($url, $param = null,$headers= null) {


		    if(is_array($param)) {
		        $query = http_build_query($param);
		        $url = $url . '?' . $query;
		    }
		    $ch = curl_init();
		    if(stripos($url, "https://") !== false){

		    	
		        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
		        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
		    }

		    curl_setopt($ch, CURLOPT_URL, $url);
		    curl_setopt ( $ch, CURLOPT_HTTPHEADER, $headers );
		    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 );
		    $content = curl_exec($ch);
		    $status = curl_getinfo($ch);
		    curl_close($ch);
		    if(intval($status["http_code"]) == 200) {
		        return $content;
		    }else{
		        echo $status["http_code"];
		        return false;
		    }
		}



	}






 ?>